package cayte.xiami.in.ui.setting;

import com.umeng.analytics.MobclickAgent;

import cayte.xiami.in.D;
import cayte.xiami.in.R;
import cayte.xiami.in.helper.PaperHelper;
import cayte.xiami.in.helper.SpfHelper;
import cayte.xiami.in.ui.TitleView;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class DetailSettingActivity extends Activity implements
		View.OnClickListener {

	private SettingItem isUseWallpaper, setPaper, isShowDetailLog;

	private SpfHelper spfHelper;

	private TitleView title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail_setting);

		spfHelper = new SpfHelper(this);

		title = new TitleView(this);
		title.setText(R.string.detailSetting);
		title.setLeftImage(R.drawable.back, new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onBackPressed();
			}
		});
		isUseWallpaper = new SettingItem(findViewById(R.id.isUseWallpaper));
		setPaper = new SettingItem(findViewById(R.id.setPaper));
		isShowDetailLog = new SettingItem(findViewById(R.id.isShowDetailLog));

		isUseWallpaper.setCheckVisible(true);
		isUseWallpaper.setTitle(R.string.isUseWallpaper);
		isUseWallpaper.setOnClickListener(this);

		setPaper.setCheckVisible(false);
		setPaper.setTitle(R.string.setPaper);
		setPaper.setOnClickListener(this);

		isShowDetailLog.setCheckVisible(true);
		isShowDetailLog.setTitle(R.string.isShowDetailLog);
		isShowDetailLog.setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
		initData();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}

	private void initData() {
		// TODO Auto-generated method stub
		if (spfHelper.isUseWallpaper()) {
			isUseWallpaper.setChecked(true);
			isUseWallpaper.setTip(R.string.open);
			setPaper.setEnabled(false);
		} else {
			isUseWallpaper.setChecked(false);
			isUseWallpaper.setTip(R.string.close);
			setPaper.setEnabled(true);
		}

		if (spfHelper.isHasPaper4Files()) {
			setPaper.setTip(R.string.setPaperTrue);
		} else {
			setPaper.setTip(R.string.setPaperFalse);
		}

		if (spfHelper.isShowDetailLog()) {
			isShowDetailLog.setChecked(true);
			isShowDetailLog.setTip(R.string.isShowDetailLogTrue);
		} else {
			isShowDetailLog.setChecked(false);
			isShowDetailLog.setTip(R.string.isShowDetailLogFalse);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.isUseWallpaper:
			boolean isUse = !isUseWallpaper.getChecked();
			spfHelper.setUseWallpaper(isUse);
			initData();
			break;
		case R.id.setPaper:
			if (spfHelper.isHasPaper4Files()) {
				PaperHelper.setPaper(this, null);
				initData();
			} else {
				doPhoto4Location();
			}
			break;
		case R.id.isShowDetailLog:
			boolean isShow = !isShowDetailLog.getChecked();
			spfHelper.setShowDetailLog(isShow);
			initData();
			break;
		default:
			break;
		}
	}

	private static final int LOC = 0;

	private void doPhoto4Location() {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(intent, LOC);
		overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case LOC:
				try {
					if (data == null)
						return;
					Uri uri = data.getData();
					if (uri == null)
						return;
					String imagePath = null;
					if (uri.getScheme().equals("content")) {
						ContentResolver cr = getContentResolver();
						Cursor mCur = cr.query(uri, null, null, null, null);
						if (mCur == null)
							return;
						if (!mCur.moveToFirst()) {
							if (mCur != null) {
								mCur.close();
								mCur = null;
							}
							return;
						}
						imagePath = mCur
								.getString(mCur.getColumnIndex("_data"));
						if (mCur != null) {
							mCur.close();
							mCur = null;
						}
					} else if (uri.getScheme().equals("file")) {
						imagePath = uri.getPath();
					}
					if (imagePath == null)
						return;
					doPhotoHandler(imagePath);
				} catch (Exception e) {
					// TODO: handle exception
					D.show(this, R.string.setPaperFailTip);
				}
			}
		}
	}

	private void doPhotoHandler(String imagePath) {
		Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
		bitmap = PaperHelper.handlerWallpaper(this, bitmap);
		PaperHelper.setPaper(this, bitmap);
		initData();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		this.finish();
		overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
	}
}
